package org.jimmy.tang.thread;

/**
 * @author jimmy
 * 
 */
public class TraditionalThreadSynchronized {
    public static void main(String[] args) {
        final Outputter output = new Outputter();
        new Thread() {
            @Override
            public void run() {
                output.output("zhangsan");
            };
        }.start();
        new Thread() {
            @Override
            public void run() {
                output.output("lisi");
            };
        }.start();
    }
}

class Outputter {
    Object lock = new Object();

    public synchronized void output(String name) {
        // 这把锁必须是线程间的共享对象，否则没有意义
        // Object lock = new Object();
        // synchronized (this) {
        // synchronized (lock) {
        for (int i = 0; i < name.length(); i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
            System.out.print(name.charAt(i));
        }
        // }
    }
}
